﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Breed;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.Breed
{
    public class CreateBreedHandler : IRequestHandler<CreateBreedCommand, APIResult<int>>
    {
        private readonly IBaseRepository<BreedingInformationModel> breedRepo;
        private readonly IMapper mapper;

        public CreateBreedHandler(IBaseRepository<BreedingInformationModel> breedRepo,
                                  IMapper mapper)
        {
            this.breedRepo = breedRepo;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(CreateBreedCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<BreedingInformationModel>(request);
            int r = breedRepo.AddAsync(model).Result;
            if (r > 0)
            {
                result.Message = "设置养殖基本信息成功";
                result.Data = r;
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "设置养殖基本信息失败";
                result.Data = r;
                result.Code = APIEnum.Fail;
            }
            return Task.FromResult(result);
        }
    }
}
